package org.lmw.note;

import java.util.Calendar;
import java.util.List;

import org.lmw.note.adapter.LogAdapter;
import org.lmw.note.common.DateUtils;
import org.lmw.note.widget.MonPickerDialog;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;

public class Act_Log extends FragmentActivity implements OnItemClickListener,OnClickListener{

	TextView mon;
	ImageButton mon_arrow_left;
	ImageButton mon_arrow_right;
	List<Note> list;
	LogAdapter adapter;
	ListView lv;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.act_log);
		initView();
	}
	
	private void initView(){
		mon=(TextView)findViewById(R.id.date_filter);
		mon_arrow_left=(ImageButton)findViewById(R.id.arrow_left);
		mon_arrow_right=(ImageButton)findViewById(R.id.arrow_right);
		
		mon.setOnClickListener(this);
		mon_arrow_left.setOnClickListener(this);
		mon_arrow_right.setOnClickListener(this);
		
		lv=(ListView)findViewById(R.id.listview_log);
		adapter=new LogAdapter(this);
		notifyData();
		
		lv.setOnItemClickListener(this);
	}
	
	private  void notifyData(){
		try {
			list.clear();
			List<Note> temp=App.db.findAll(Selector.from(Note.class).where("status", "=", 4).orderBy("createTime",true));
			list.addAll(temp);
		} catch (DbException e) {
			e.printStackTrace();
		}
		adapter.notifyDataSetChanged();
	}
	
	/*
	 * 日期选择器 年-月
	 */
	public void showMonPicker() {
		final Calendar localCalendar = Calendar.getInstance();
		localCalendar.setTime(DateUtils.strToDate("yyyy-MM", mon.getText().toString()));
		new MonPickerDialog(this,new DatePickerDialog.OnDateSetListener() {
					@Override
					public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
						localCalendar.set(1, year);
						localCalendar.set(2, monthOfYear);
						mon.setText(DateUtils.clanderTodatetime(localCalendar, "yyyy-MM"));
						notifyData();
					}
				}, localCalendar.get(1), localCalendar.get(2),localCalendar.get(5)).show();
	}
	/*
	 * 上一月/下一月
	 */
	public void setMon(int val){
		mon.setText(DateUtils.clanderTodatetime(changeMon(val), "yyyy-MM"));
		notifyData();
	}

	public Calendar changeMon(int val){
		Calendar calend = Calendar.getInstance();
		calend.setTime(DateUtils.strToDate("yyyy-MM", mon.getText().toString()));
		calend.add(Calendar.MONTH, val);
		return calend;
		
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		
	}

	@Override
	public void onClick(View v) {
		if(v==mon)
			showMonPicker();
		if(v==mon_arrow_left)
			setMon(-1);
		if(v==mon_arrow_right)
			setMon(1);
	}
}
